<template>
  <div class="app-container glories-container">
    <!-- 选择 -->
    <div class="filter-container">
      <el-input v-model="paging.keywords" placeholder="请输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="reportList" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="reportList">搜索</el-button>
    </div>

    <!--表格-->
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="举报人" min-width="100px" align="center">
        <template slot-scope="scope">
          <el-tag type="danger">{{ scope.row.userReport.name }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="举报标题" min-width="100px" align="center">
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.title }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="举报内容" min-width="100px" align="center">
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.desc }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="举报时间" min-width="100px" align="center">
        <template slot-scope="scope">
          <el-tag type="success">{{ renderTime(scope.row.createdAt) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="100px" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="details(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="paging.count>0" :total="paging.count" :page.sync="paging.page" :limit.sync="paging.limit" @pagination="reportList" />

    <!-- 弹框 -->
    <el-dialog
      v-loading="listLoading"
      :title="alert.title"
      :visible.sync="alert.centerDialogVisible"
      :before-close="close"
      width="30%"
    >

      <h2>{{ info.title }}</h2>
      <p>{{ info.desc }}</p>
      <video v-if="info.video" :src="info.video" controls width="60%" style="display: block;margin: auto;" />
      <el-carousel v-if="imgList.length">
        <el-carousel-item v-for="item in imgList" :key="item" style="text-align: center;">
          <img :src="item" style="height:100%;">
        </el-carousel-item>
      </el-carousel>
      <span slot="footer" class="dialog-footer">
        <el-button type="danger" @click="del">删 除</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves'
import { reportList, reportDetails, reportDel } from '@/api/set'
import Pagination from '@/components/Pagination'
import { renderTime } from '../../utils'

export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      listLoading: false,
      list: null,
      paging: {
        count: 0,
        page: 1,
        limit: 10,
        keywords: undefined
      },
      alert: {
        title: '举报详情',
        centerDialogVisible: false
      },
      info: {},
      imgList: [],
      postList: {}

    }
  },
  created() {
    this.reportList()
  },
  methods: {
    renderTime,
    // 获取表格数据
    reportList() {
      this.listLoading = true
      reportList(this.paging).then(res => {
        const { data: { count, rows }} = res.data
        this.paging.count = count
        this.list = rows
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    details(resp) {
      this.postList = resp
      this.alert.centerDialogVisible = true
      reportDetails(resp.postId).then(res => {
        const { data } = res.data
        if (data === null) {
          this.alert.centerDialogVisible = false
          this.$message({
            type: 'danger',
            message: '暂无详情内容'
          })
          return
        }
        this.info = data
        this.imgList = data.images.split(',')
      }).catch(() => {})
    },
    del() {
      this.$confirm('是否删除当前举报的内容?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        reportDel(this.postList.id, { pId: this.postList.postId }).then(res => {
          console.log(res)
          const { msg } = res.data
          this.$message({
            type: 'success',
            message: msg
          })
          this.alert.centerDialogVisible = false
          this.reportList()
        }).catch(() => {})
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    close() {
      this.alert.centerDialogVisible = false
    }
  }
}
</script>
